package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.ArticleReplyMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.Article;
import com.aaa.forum.model.ArticleReply;
import com.aaa.forum.services.IArticleReplyService;
import com.aaa.forum.services.IArticleService;
import com.aaa.forum.uitls.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;

    @Override
    public void create(ArticleReply articleReply) {
        if (articleReply == null || articleReply.getArticleId() == null
                || articleReply.getUserId() == null
                || StringUtil.isEmpty(articleReply.getContent())) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        // 写入数据库
        int row = articleReplyMapper.insertSelective(articleReply);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        // 更新帖子表的回复数量
        articleService.addOneReplyCountById(articleReply.getArticleId());

        log.info("回复成功, id = " + articleReply.getId()
                + "articleId = " + articleReply.getArticleId()
                + ", userId = " + articleReply.getUserId());
    }

    @Override
    public List<ArticleReply> selectAllByArticleId(Long articleId) {
        // 非空校验
        if (articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        // 调用dao
        return articleReplyMapper.selectAllByArticleId(articleId);
    }

    @Override
    public void addOneLikeCountById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 获取评论信息
        ArticleReply articleReply = articleReplyMapper.selectByPrimaryKey(id);
        if (articleReply == null || articleReply.getDeleteState() == 1) {
            // 评论不存在 或 已删除
            log.warn(ResultCode.FAILED_REPLY_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_REPLY_NOT_EXISTS));
        }

        // 构造更新对象
        ArticleReply updateReply = new ArticleReply();
        updateReply.setId(articleReply.getId());
        updateReply.setLikeCount(articleReply.getLikeCount() + 1);
        // 调用 dao, 更新数据库
        int row = articleReplyMapper.updateByPrimaryKeySelective(updateReply);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void subOneLikeCountById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 获取评论信息
        ArticleReply articleReply = articleReplyMapper.selectByPrimaryKey(id);
        if (articleReply == null || articleReply.getDeleteState() == 1) {
            // 评论不存在 或 已删除
            log.warn(ResultCode.FAILED_REPLY_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_REPLY_NOT_EXISTS));
        }

        // 构造更新对象
        ArticleReply updateReply = new ArticleReply();
        updateReply.setId(articleReply.getId());
        updateReply.setLikeCount(articleReply.getLikeCount() - 1);
        if (updateReply.getLikeCount() < 0) updateReply.setLikeCount(0);
        // 调用 dao, 更新数据库
        int row = articleReplyMapper.updateByPrimaryKeySelective(updateReply);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public ArticleReply selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return articleReplyMapper.selectByPrimaryKey(id);
    }
}
